package com.yanqu.road.server.manager.activity.horserun;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 小马快跑跨服管理类
 */

public class CrossHorseRunMgr extends TempCrossMgr {

    //活动id 分组活动
    public static Map<Integer,CrossHorseRunGroup> groupMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> needRemoveList = new ArrayList<>();
        long currentSecond = DateHelper.getCurrentTime() / 1000;
        for (CrossHorseRunGroup group:groupMap.values()){
            HorseRunConfig config = CrossHorseRunConfigMgr.getHorseRunConfig(group.getActivityId());
            if (config == null || currentSecond > config.getActivityInfo().getEndShowTime() + 24 * DateHelper.HOUR_SECONDS){
                needRemoveList.add(group.getActivityId());
            }
        }

        for (Integer activityId:needRemoveList){
            groupMap.remove(activityId);
        }
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.HorseRun.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        for (ActivityInfo activityInfo:tempActivityInfoMap.values()){
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap) {
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                        getActivity(activityInfo.getActivityId(), activityGroup.getGroupId());
                    }
                }
            }
        }
        return true;
    }

    public static CrossHorseRunGroup getGroup(int activityId){
        CrossHorseRunGroup group = groupMap.get(activityId);
        if (null == group) {
            synchronized (groupMap) {
                group = groupMap.get(activityId);
                if (null == group) {
                    HorseRunConfig horseRunConfig = CrossHorseRunConfigMgr.getHorseRunConfig(activityId);
                    if (horseRunConfig == null) {
                        horseRunConfig = CrossHorseRunConfigMgr.reloadHorseRunConfig(activityId);
                    }
                    if (null == horseRunConfig) {
                        return null;
                    }

                    group = new CrossHorseRunGroup(activityId);
                    groupMap.put(activityId, group);
                }
            }
        }
        return group;
    }

    /**
     * 定时器
     */
    public static void runTask(){
        if (groupMap == null || groupMap.isEmpty()) {
            return;
        }
        for (CrossHorseRunGroup group:groupMap.values()){
            group.timerTask();
        }
    }


    /**
     * 获取活动
     * @param activityId
     * @param groupId
     * @return
     */

    public static CrossHorseRunActivity getActivity(int activityId, int groupId){
        CrossHorseRunGroup group = getGroup(activityId);
        if (null == group) {
            return null;
        }
        return group.getActivity(groupId);
    }

    @Override
    public boolean save() {
        for (CrossHorseRunGroup group:groupMap.values()){
            group.save();
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
